<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2021/4/21
 * Time: 10:53 上午
 */

namespace app\common\cachemodel;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

/**
 * Class MapUnlockResult
 * @package app\common\cachemodel
 */
class MapUnlockResult extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_map_unlock_result';
    protected $cacheModel = 'map.map_unlock_result';

    /**
     * 添加结果数据
     * @param $data
     * @return bool
     */
    public function saveData($data)
    {
        try
        {
            $this->saveAll($data);
            $datum = current($data);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $datum['order_id'])->setListKeyType($datum['type'])->rmCache('list');
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $datum['rid'])->setListKeyType(3)->rmCache('list');
        }
        catch (\Exception $e)
        {
            return false;
        }
        return true;
    }

    /**
     * 更新结果数据
     * @param $ids
     * @param $data
     * @return bool
     */
    public function updateData($ids, $data)
    {
        try
        {
            $this->where(['id' => ['in', $ids]])->update($data);
            $cacheHandle = CacheManage::getInstance();
            foreach ($ids as $id)
            {
                $cacheHandle->setCacheConfig($this->cacheModel, $id)->rmCache();
            }
        }
        catch (\Exception $e)
        {
            return false;
        }
        return true;
    }

    /**
     * 获取地图解锁结果信息Id
     * @param $type 1=>订单 2=>赠送 3=>档案
     * @param $dataId
     * @return array
     * @throws \Exception
     */
    public function getUnlockIdsFromCache($type, $dataId)
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel, $dataId)->setListKeyType($type)->setSort('ASC')->ZsGetIocData('list', function () use ($type, $dataId)
        {
            return $this->getUnlockIdsFromDms($type, $dataId);
        });
    }

    /**
     * 数据库获取不同类型的推荐id
     * @param $type
     * @param $dataId
     * @return array
     */
    public function getUnlockIdsFromDms($type, $dataId)
    {
        if ($type === 3)
        {
            $where = ['rid' => $dataId];
        } else
        {
            $where = ['type' => $type, 'order_id' => $dataId];
        }
        try
        {
            $data = $this->where($where)->order('id', 'DESC')->column('id', 'id');
            return array_flip_key_value($data);
        }
        catch (\Exception $e)
        {
            return [];
        }
    }

    /**
     * 批量获取地图解锁结果信息
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this->where(['id' => ['in', $ids]])->column('type,order_id,rid,gid,status,rid_back,line_planet_info_id,last_calculation_time', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }
}
